package com.vcredit.creditcat.web.app;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vcredit.creditcat.api.IEduQueryManager;
import com.vcredit.creditcat.dto.eduquery.AuthSchoolDto;
import com.vcredit.creditcat.dto.eduquery.EduInitDto;
import com.vcredit.creditcat.dto.eduquery.EduQueryParam;
import com.vcredit.creditcat.exception.CustLoginException;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.vo.EduInitVo;
import com.vcredit.creditcat.vo.JsonResult;
import com.vcredit.framework.annotation.Log;

/**
 * 学信网查询
 * 
 * @author zhangboshi
 *
 */
@RestController
@RequestMapping("/education")
public class EduQueryController extends BaseCreditCatController {

	@Reference(version = "1.0")
	private IEduQueryManager eduQueryManager;

	/**
	 * 学信网查询初始化
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Log
	@RequestMapping(value = "/init")
	public JsonResult<EduInitVo> eduQuertInit(HttpServletRequest request) throws Exception {
		Cust currentCust = getCust(request);
		if (null == currentCust) {
			throw new CustLoginException("无用户登录或者未身份认证");
		}
		EduInitDto eduInit = eduQueryManager.init(currentCust);
		return JsonResult.SuccessResponse(new EduInitVo(eduInit.getToken(), eduInit.getVerCodeBase64()));
	}

	/**
	 * 学信网查询
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Log
	@RequestMapping(value = "/query")
	public JsonResult<AuthSchoolDto> eduQuert(@RequestBody @Valid EduQueryParam param, HttpServletRequest request)
			throws Exception {
		Cust currentCust = getCust(request);
		if (null == currentCust) {
			throw new CustLoginException("无用户登录或者未身份认证");
		}
		param.setIdentityCard(currentCust.getCustIdno());
		param.setMobileNo(currentCust.getCustMobile());
		param.setProccessId(currentCust.getLastProccessId());
		AuthSchoolDto authSchool = eduQueryManager.eduQuery(param, currentCust);
		return JsonResult.SuccessResponse(authSchool);
	}
}
